<?php
class UserController extends Controller{
	
	public function filters(){
		return array(
				'accessControl - logout - login - captcha'
		);
	}
	
	public function accessRules(){
		return array(
				array(
						'allow',
						'actions'=>array('add','list','edit','changePwd','delete'),
						'users'=>array('@')
				),
				array(
						'deny',
						'users'=>array('*')
				)
		);
	}

	public function actions(){
		return array(
				'captcha'=>array(
					'class'=>'Captcha',
					'maxLength'=>4,
					'minLength'=>2,
					'width'=>150,
					'height'=>40
				)
			);
	}

/**
 * 用户登录
 * @return [type] [description]
 */
	public function actionLogin(){
		$loginForm = new LoginForm();
		if(isset($_POST['LoginForm'])){
			$loginForm->attributes = $_POST['LoginForm'];
			if($loginForm->validate() && $loginForm->login()){
				$this->redirect(array('default/index'));
			}
		}
		$this->title = '用户登录';
		$this->layout = '/layouts/noheader';
		$this->render('login', array('loginForm'=>$loginForm));
	}

/**
 * 用户登出
 * @return [type] [description]
 */
	public function actionLogout(){
		Yii::app()->user->logout();
		$this->redirect(array('login'));
	}

/**
 * 添加管理员
 * @return [type] [description]
 */
	public function actionAdd(){
		$userModel = new User('create');
		if(isset($_POST['User'])){
			$userModel->attributes = $_POST['User'];
			if($userModel->save()){
				$this->redirect(array('list'));
			}
		} else {
			$userModel->status = 1;
		}
		$this->render('add', array('userModel'=>$userModel));
	}

/**
 * 查看管理员列表
 * @return [type] [description]
 */
	public function actionList(){
		$sql = "SELECT * FROM {{user}}";
		$userList = User::model()->findAllBySql($sql);
		$this->render('list', array('userList'=>$userList));
	}

/**
 * 编辑用户信息
 * @param  [type] $id [description]
 * @return [type]     [description]
 */
	public function actionEdit($user_id){
		$userModel = User::model();
		$userInfo = $userModel->findByPk($user_id);
		if(isset($_POST['User'])){
			$userInfo->attributes = $_POST['User'];
			if($res = $userInfo->save()){
				$this->redirect(array('list'));
			}
		}
		$this->render('edit', array('userModel'=>$userInfo));
	}

/**
 * 修改密码
 * @return [type] [description]
 */
	public function actionChangePwd(){
		$model = new ChangepwdForm();
		if(isset($_POST['ChangepwdForm'])){
			//压入数据
			$model->attributes = $_POST['ChangepwdForm'];
			$vRes = $model->validate();
			if($vRes){
				$userInfo = User::model()->find('username=:username', array(':username'=>Yii::app()->user->name));
				$newpassword = md5($_POST['ChangepwdForm']['newpassword']);
				$res = User::model()->updateByPk($userInfo->user_id, array('password'=>$newpassword));
				Yii::app()->user->setFlash('changeSuccess', '密码修改成功');
			}
		}

		$this->render('changePwd', array('model'=>$model));
	}

/**
 * 删除用户
 * @param  [type] $id [description]
 * @return [type]     [description]
 */
	public function actionDelete(){
		if(Yii::app()->request->isAjaxRequest){
			$userId = Yii::app()->request->getPost('user_id');
			
			//是否存在用户文章
			$count = Article::model()->count('user_id = :user_id', array(':user_id'=>$userId));
			if($count > 0){
				echo json_encode(array('code'=>0, 'message'=>'该用户下还有文章，禁止删除'));
				die();
			}
			
			//删除用户
			$res = User::model()->deleteByPk($userId);
			if($res){
				echo json_encode(array('code'=>1));
			} else {
				echo json_encode(array('code'=>0, 'message'=>'删除失败'));
			}
		}
	}
}